package xiangmu

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
)

type XiangmusRBody struct {
	Data       []models.Xiangmu `json:"data"`
	PageNo     int              `json:"pageNo"`
	PageSize   int              `json:"pageSize"`
	TotalCount int64            `json:"totalCount"`
	TotalPage  int64            `json:"totalPage"`
}

type XiangmusBody struct {
	Page         int    `json:"pageNo" from:"pageNo"`
	Pagesize     int    `json:"pageSize" from:"pageSize"`
	ID           int64  `json:"id" gorm:"id"`
	Xiangmuid    int64  `json:"xiangmuid" gorm:"xiangmuid"`
	Name         string `json:"name" gorm:"name"`
	Adminid      int64  `json:"adminid" gorm:"adminid"`
	Chengyuanids string `json:"chengyuanids" gorm:"chengyuanids"`
}

func Xiangmucys(ctx *gin.Context) {
	request := XiangmusBody{}
	ctx.ShouldBind(&request)
	xiangmuid := request.Xiangmuid
	var xiangmucy []models.Xiangmucy
	data_source.Db.Where("xiangmuid = ?", xiangmuid).Find(&xiangmucy)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    xiangmucy,
		"msg":     "ok",
	})
	return

}
func Xiangmuupdate(ctx *gin.Context) {
	request := XiangmusBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	adminid := request.Adminid
	chengyuanids := request.Chengyuanids
	var chengyuanarr []string
	chengyuanidarr := strings.Split(chengyuanids, ",")
	var admins []models.Adminuser
	data_source.Db.Where("id in (?)", chengyuanidarr).Find(&admins)
	for _, a := range admins {
		chengyuanarr = append(chengyuanarr, a.Name)
	}
	xiangmuadd := models.Xiangmu{
		Name:       name,
		Addtime:    time.Now().Unix(),
		Adminid:    adminid,
		Chengyuans: strings.Join(chengyuanarr, ","),
	}
	data_source.Db.Where("id = ?", id).Updates(&xiangmuadd)
	data_source.Db.Where("xiangmuid = ?", id).Delete(&models.Xiangmucy{})
	var chengyuans []models.Xiangmucy
	for _, v := range admins {
		cy := models.Xiangmucy{
			Xiangmuid: id,
			Adminid:   v.ID,
			Adminname: v.Name,
		}
		chengyuans = append(chengyuans, cy)
	}
	data_source.Db.Create(&chengyuans)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Xiangmuadd(ctx *gin.Context) {
	request := XiangmusBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	adminid := request.Adminid
	chengyuanids := request.Chengyuanids
	var chengyuanarr []string

	chengyuanidarr := strings.Split(chengyuanids, ",")
	var admins []models.Adminuser
	data_source.Db.Where("id in (?)", chengyuanidarr).Find(&admins)
	for _, a := range admins {
		chengyuanarr = append(chengyuanarr, a.Name)
	}
	xiangmuadd := models.Xiangmu{
		Name:       name,
		Addtime:    time.Now().Unix(),
		Adminid:    adminid,
		Chengyuans: strings.Join(chengyuanarr, ","),
	}
	data_source.Db.Create(&xiangmuadd)
	var chengyuans []models.Xiangmucy
	for _, v := range admins {
		cy := models.Xiangmucy{
			Xiangmuid: xiangmuadd.ID,
			Adminid:   v.ID,
			Adminname: v.Name,
		}
		chengyuans = append(chengyuans, cy)
	}
	data_source.Db.Create(&chengyuans)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Xiangmus(ctx *gin.Context) {
	request := XiangmusBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	//var users []models.Procure
	var users []models.Xiangmu
	res := data_source.Db
	res.Model(&models.Xiangmu{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := XiangmusRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
